from flask_restful import Resource, reqparse,request
from ....Model.Models import Information as InformationModel
from ....utils import timestamp ,response
from flask_restful import reqparse, abort, Api, Resource, fields,\
    marshal_with
from flask_restful_swagger import swagger
# information_poot_parser = reqparse.RequestParser()
# information_poot_parser.add_argument('title', type=str)
# information_poot_parser.add_argument('content', type=str)
# information_poot_parser.add_argument('picture', type=str)
# information_poot_parser.add_argument('create_user_id', type=int)
# information_poot_parser.add_argument('support', type=int)
# information_poot_parser.add_argument('unsupported', type=int)
# information_poot_parser.add_argument('postcodes', type=int)
# information_poot_parser.add_argument('place', type=str)
# information_poot_parser.add_argument('coordinate_x', type=str)
# information_poot_parser.add_argument('coordinate_y', type=str)
# information_poot_parser.add_argument('state', type=int)

'''
审核 = 0
上线 = 1
下线 = 2
'''
examine = 0
Online = 1
Offline = -1

success = 1
notfond = -1
error = 0
class Informationlist(Resource):

    def __init__(self, db, auth=None):
        self.db = db

    def get(self):
        try:
             return response(success ,None, [each.as_dict() for each in self.db.session.query(InformationModel).all()])
        except Exception as er:
            return response(error,er,None)

    def post(self):
        try:
            args = request.form
            self.db.session.add(
                InformationModel(
                    title=args['title'],
                    content=args['content'],
                    picture=args['picture'],
                    create_user_id=args['create_user_id'],
                    support=args['support'],
                    unsupported=args['unsupported'],
                    postcodes=args['postcodes'],
                    place=args['place'],
                    coordinate_x=args['coordinate_x'],
                    coordinate_y=args['coordinate_x'],
                    state=examine,
                    update_at=timestamp(),
                    create_at=timestamp()
                )
            )
            self.db.session.commit()
            return response(success,None,None)
        except Exception as er :
            # 错误自己写
            return response(error,er,None)

class Information(Resource):
    def __init__(self, db, auth=None):
        self.db = db
    def get(self, article_id):
        try:
            return response(success,None, self.db.session.query(InformationModel).filter_by(id=article_id).first().as_dict())
        except Exception as er:
            return response(error,er,None)

    def delete(self, article_id):
        try:
            information_obj = self.db.session.query(InformationModel).filter_by(id=article_id).first()
            print(information_obj.as_dict())
            if(information_obj == None):
                return {'msg': notfond}
            self.db.session.delete(information_obj)
            self.db.session.commit()
            return response(success,None,None)
        except Exception as er:
            return response(error,er,None)


    def put(self, article_id):
        try:
            args = request.form
            Information_obj = self.db.session.query(InformationModel).filter_by(id=article_id).first()
            if(args['title'] != None):
                Information_obj.title = args['title']
            if(args['content'] != None):
                Information_obj.content = args['content']
            if(args['picture'] != None):
                Information_obj.picture = args['picture']
            if(args['create_user_id'] != None):
                Information_obj.create_user_id = args['create_user_id']
            if(args['support'] != None):
                Information_obj.support = args['support']
            if(args['unsupported'] != None):
                Information_obj.unsupported = args['unsupported']
            if(args['postcodes'] != None):
                Information_obj.postcodes = args['postcodes']
            if(args['place'] != None):
                Information_obj.title = args['place']
            if(args['title'] != None):
                Information_obj.title = args['title']
            if(args['coordinate_x'] != None):
                Information_obj.support = args['coordinate_x']
            if(args['coordinate_y'] != None):
                Information_obj.support = args['coordinate_y']
            if(args['state'] != None):
                Information_obj.state = args['state']
            Information_obj.update_at = timestamp()
            self.db.session.commit()
            return response(success,None,None)
        except Exception as er:
            return response(error,er,None)